package com.cap.bts.crmdp.dealer.coupon.dao.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.coupon.constants.CouponConstants;
import com.cap.bts.crmdp.dealer.coupon.dao.ITicKetDefineDao;
import com.cap.bts.crmdp.dealer.coupon.domain.Ticket;
import com.cap.bts.crmdp.dealer.coupon.dto.TicKetDefineDto;
import com.cap.bts.framework.common.dao.callback.NativeQueryWithPagingJpaCallbackForJavaDomain;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Pager;

/**
 * 优惠券和提货券管理
 * @author lizhiyong 
 * @version $Id: TicKetDefineImpl.java, v 0.1 2015-9-10 下午1:32:05 1111 Exp $
 */
@Repository("iTicKetDefineDao")
public class TicKetDefineDaoImpl extends BaseDaoImpl<Ticket, Long> implements ITicKetDefineDao {

    @Inject
    public TicKetDefineDaoImpl(@Value("com.cap.bts.crmdp.dealer.coupon.domain.Ticket") Class<Ticket> clazz) {
        super(clazz);
    }

    /**
     * 新增提货券和优惠券信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicKetDefineDao#insertTicketInfo(com.cap.bts.crmdp.dealer.coupon.domain.Ticket)
     */
    @Override
    public Ticket insertTicketInfo(Ticket ticket) throws AppException {
    	if(ticket.getMaximunTimes() == null || ticket.getMaximunTimes().intValue() < 1){
    		ticket.setMaximunTimes(1);
    	}else{
    		ticket.setMaximunTimes(ticket.getMaximunTimes());
    	}
        return save(ticket);
    }

    /**
     * 提货券和优惠券信息查询
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicKetDefineDao#getTicketList(com.cap.bts.crmdp.dealer.coupon.dto.TicKetDefineDto)
     */
    @Override
    public List<Ticket> getTicketList(TicKetDefineDto ticKetDefineDto) throws AppException {
        String selectStr = "select * from tm_raw_ticket_def";
        String countStr = "select count(*) from tm_raw_ticket_def";
        //查询结果集的SQL
        StringBuilder selectSql = new StringBuilder();
        //查询结果集的记录数的SQL
        StringBuilder countSql = new StringBuilder();

        StringBuilder where = new StringBuilder();
        where.append(" where 1=1 ");
        if (ticKetDefineDto.getTicketName() != null && !"".equals(ticKetDefineDto.getTicketName())) {
            where.append("and ticket_name like '%" + ticKetDefineDto.getTicketName() + "%'");
        }
        String ticketType = ticKetDefineDto.getTicketType();
        if (ticketType != null && !"".equals(ticketType) && !"N/A".equals(ticketType)) {
            where.append("and ticket_type='" + ticKetDefineDto.getTicketType() + "'");
        }
        String order = " order by id desc";

        selectSql.append(selectStr).append(where.toString()).append(order);
        countSql.append(countStr).append(where.toString());

        Pager page = ticKetDefineDto.getPager();
        page.setPageSize(10);

        List<Ticket> list = super.getJpaTemplate().execute(
            new NativeQueryWithPagingJpaCallbackForJavaDomain<Ticket>(selectSql.toString(),
                countSql.toString(), page, Ticket.class));
        ticKetDefineDto.setPager(page);

        return list;
    }

    /**
     * 取得优惠券定义列表
     * overridden:
     * @Date        :      Sep 10, 2015
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicKetDefineDao#getCouponTicketList()
    **/
    @Override
    public List<Ticket> getCouponTicketList() throws AppException {
        return super.getListByQueryWithVaParam("from Ticket a where a.ticketType=?1",
            CouponConstants.TICKET_TYPE_COUPON);
    }

    /**
     * 根据优惠券名称查询信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicKetDefineDao#getTicketByTicketName(com.cap.bts.crmdp.dealer.coupon.dto.TicKetDefineDto)
     */
    @Override
    public boolean getTicketByTicketName(TicKetDefineDto ticKetDefineDto) throws AppException {
        String selectStr = "select * from tm_raw_ticket_def where ticket_name=?1 and ticket_type=?2";

        List<Ticket> list1 = super.getListByNativeQueryWithVaParam(selectStr,
            ticKetDefineDto.getTicketName(), ticKetDefineDto.getTicketType());
        if (!list1.isEmpty()) {
            Ticket ticket = list1.get(0);
            if (ticket != null) {
                return false;
            }
        }
        return true;
    }

    /**
     * 根据优惠券和提货券ID查询信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicKetDefineDao#getTicketById(com.cap.bts.crmdp.dealer.coupon.dto.TicKetDefineDto)
     */
    @Override
    public Ticket getTicketById(String id) throws AppException {
        String selectStr = "select * from tm_raw_ticket_def where id=?1";

        Ticket resTicket = null;

        List<Ticket> list1 = super.getListByNativeQueryWithVaParam(selectStr, id);
        if (!list1.isEmpty()) {
            resTicket = list1.get(0);
        }
        return resTicket;
    }

    /**
     * 查询所有的优惠券和提货券信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicKetDefineDao#getAllTicketList()
     */
    @Override
    public List<Ticket> getAllTicketList() throws AppException {
        String selectStr = "select * from tm_raw_ticket_def";
        return super.getListByNativeQueryWithVaParam(selectStr);

    }

    /**
     * 修改优惠券和提货券信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicKetDefineDao#updateCouponInfo(com.cap.bts.crmdp.dealer.coupon.dto.TicKetDefineDto)
     */
    @Override
    public boolean updateTicketInfo(TicKetDefineDto ticKetDefineDto) throws AppException {
        Ticket ticket = findById(ticKetDefineDto.getId());

        ticket.setTicketName(ticKetDefineDto.getTicketName());
        ticket.setTicketType(ticKetDefineDto.getTicketType());
        ticket.setVerificationMethod(ticKetDefineDto.getVerificationMethod());
        ticket.setDeductionAmount(ticKetDefineDto.getDeductionAmount());
        ticket.setDeductionMethod(ticKetDefineDto.getDeductionMethod());
        
        	if(ticKetDefineDto.getMaximunTimes() == null || ticKetDefineDto.getMaximunTimes().intValue() < 1){
        		ticket.setMaximunTimes(1);
        	}else{
        		ticket.setMaximunTimes(ticKetDefineDto.getMaximunTimes());
        	}
        
        if (CouponConstants.TICKET_TYPE_COUPON.equals(ticKetDefineDto.getTicketType())) {//优惠券
            ticket.setSmsRemind(ticKetDefineDto.getSmsRemind());
        } else if (CouponConstants.TICKET_TYPE_REDEMPTION.equals(ticKetDefineDto.getTicketType())) {//提货券
            ticket.setIfPrepay(ticKetDefineDto.getIfPrepay());
        }
        update(ticket);
        return true;
    }

    /**
     * 根据优惠券名称查询信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicKetDefineDao#getTicketByTicketName2(com.cap.bts.crmdp.dealer.coupon.dto.TicKetDefineDto)
     */
    @Override
    public Ticket getTicketByTicketName2(TicKetDefineDto ticKetDefineDto) throws AppException {
        String selectStr = "select * from tm_raw_ticket_def where ticket_name=?1 and ticket_type=?2";

        List<Ticket> list1 = super.getListByNativeQueryWithVaParam(selectStr,
            ticKetDefineDto.getTicketName(), ticKetDefineDto.getTicketType());
        if (!list1.isEmpty()) {
            return list1.get(0);
        }
        
        return null;
    }

}
